﻿
  var map;
  
  var latlonServidor = new Array( );
  
  var aMarkets = new Array( );
  
  var datosServidor = new Array( );
      
  var aTrack = new Array( );

  function initialize( ) {   
  
      var settings = { 
		    zoom: 13,
		    center: new google.maps.LatLng(37.4419, -122.1419),
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		    
	      };
	      
	  map = new google.maps.Map(document.getElementById("map"), settings);
	  	  	  
   }  
   
   function fntDatosServidor(datos){ 
        
     if(""+datos.length <= 0) return;
    
     datosServidor =  datos.split(";");
     
     var arregloTrack = new Array( );
    
     for (var i=0; i < datosServidor.length ; i++) {
            var arrCoordenada = datosServidor[i].split("|"); 
            var lat = arrCoordenada[0];
            var lon = arrCoordenada[1]; 
                   
           arregloTrack[i] = new google.maps.LatLng(eval(lat), eval(lon));
           createMarker( i, lat, lon );
           
           if(!isultimaPosicion( ))
            {
            crearPolyline(datosServidor[i]);
            }
     }
  
     if(!isultimaPosicion( ))
     {
        var flightPath = new google.maps.Polyline({
        path: arregloTrack,
        strokeColor: "#ff0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
       });

      flightPath.setMap(map);
      
      aTrack.push(flightPath);
     }
     
  } 

  function createMarker(sec, lat, lon) { 
  	   
  	   var marker = new google.maps.Marker({
          position: new google.maps.LatLng(eval(lat), eval(lon)), 
          map: map
       });
          
      latlonServidor[sec] = lat+'|'+lon;   
      aMarkets[sec] = marker; 
      
      var infowindow = new google.maps.InfoWindow({
        content: "<div id='infos'>"+ parsearDatosShowRepetidos(sec, lat, lon)+ "</div>"
       });
      
      google.maps.event.addListener(marker, "click", function( ) {
        infowindow.open(map, marker);
      });
      return marker;
    }
    
    function mostrarImagen(  ){
   
       $(".image").hover(function( ) {                   
        var image = $(this).attr("rel");
        $('#imagenes').fadeIn();
        $('#imagenes').html('<img src="' + image + '"/>');
            return false;
        });
    }
    
    
    function trim(str){
        str = str.replace(/^\s*|\s*$/g,"");
        return str;
    }                       
    
    
    function parsearDatosShowRepetidos(sec, lat, lon){  
    
        var cadena ="";
    	
        var repetidos = buscarPuntosRepetidos(sec, lat+'|'+lon);
        
	        if(repetidos.length > 0){
		        
		        for(var i=0; i < repetidos.length; i++){
		            
		           cadena = cadena + 
		                    " <div class='section1'>" +
		                    "<a href='#' style='cursor:pointer; text-decoration:none; color: #000000;' onmouseover='aHover(this);' onclick='showMarkerRepeat(" + repetidos[i] + ");'>" + 
		                    "SECUENCE : " + ( eval(repetidos[i]) + 1 )+" </a> </div>" ;
		            
		        }
	        }else{
        	         cadena = showMessageDialog(sec, datosServidor[sec]);
	        }  
	     
         return cadena;
   }
   
   function buscarPuntosRepetidos(sec, latlon){
			
			var arregloPuntosRepetidos = new Array( );
			
			 for(var i=0; i<latlonServidor.length; i++){
		        //busca repetidos excepto el suyo
				if(latlonServidor[i] == latlon && sec != i){
					arregloPuntosRepetidos[arregloPuntosRepetidos.length] = i;
				}
			}
			//pone el suyo propio
			if(arregloPuntosRepetidos.length > 0 ){
				arregloPuntosRepetidos[arregloPuntosRepetidos.length] = sec;
			} 
			
			return arregloPuntosRepetidos;
			
   }
   
   function crearPolyline(datosTemporales){
        
        var arrDatosTotal = datosTemporales.split("$$"); 
			
		if( !(arrDatosTotal.length >1 && arrDatosTotal[1] !=  '')){
		    return;
		}

       var tmpArray = arrDatosTotal[1].split("@"); 
      
       var arregloTemp = new Array( );
       
       for (var i=0; i < tmpArray.length ; i++) {
            var arrCoordenada = tmpArray[i].split("|"); 
            var lat = arrCoordenada[0];
            var lon = arrCoordenada[1]; 
          
           arregloTemp[i] = new google.maps.LatLng(eval(lat), eval(lon));
     }
    
 
      var flightPath = new google.maps.Polyline({
        path: arregloTemp,
        strokeColor: "#00ff00",
        strokeOpacity: 1.0,
        strokeWeight: 2
      });

      flightPath.setMap(map);
      
      aTrack.push(flightPath);
     
  }
